/*
 * Copyright (c) ycq_work@163.com
 */

package com.mxg.blog.controller.articleAdmin;

import com.mxg.blog.common.CqLogger;
import com.mxg.blog.common.DetermineLogin;
import com.mxg.blog.common.ResultCode;
import com.mxg.blog.entity.ArticleCategory;
import com.mxg.blog.service.ArticleCategoryService;
import com.mxg.blog.service.UserService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: ycq_work@163.com
 * @DATE: 2023/5/30 3:14
 * 获取所有文章分类
 */
@Controller
public class GetAllArticleCategory {
    private final ArticleCategoryService articleCategoryService;
    private final UserService userService;
    CqLogger cqLogger = CqLogger.getInstance();

    public GetAllArticleCategory (ArticleCategoryService articleCategoryService, UserService userService)
    {
        this.articleCategoryService = articleCategoryService;
        this.userService = userService;
    }

    @PostMapping("api/getAllArticleCategory")
    public ResponseEntity<Map<String, Object>>getAllArticleCategory(@RequestBody Map<String, String> requestMap){
        Map<String, Object> resultMap = new HashMap<>();

        cqLogger.print(requestMap);

        // 是否登录验证
        DetermineLogin determineLogin = DetermineLogin.getInstance(requestMap.get("userAccount"), userService);

        if(determineLogin.startJudge().equals(DetermineLogin.NOT_LOGIN)){
            resultMap.put(ResultCode.code, ResultCode.WRONG_CODE_NOT_LOGIN);
            resultMap.put(ResultCode.message, ResultCode.WRONG_MESSAGE_NOT_LOGIN);
            return ResponseEntity.ok(resultMap);
        }else {
            cqLogger.print("验证成功");
        }

        // 生命分类列表
        List<ArticleCategory> articleCategoryList = articleCategoryService.getArticleCategory();

        // 获取所有的分类
        if(!articleCategoryList.isEmpty()){
            List<Map<String, Object>> tableData = new ArrayList<>();
            // 将获取的列表信息存入
            for (ArticleCategory articleCategory : articleCategoryList) {
                Map<String, Object> articleCategoryInfo = new HashMap<>();

                cqLogger.print("获取成功");
                articleCategoryInfo.put(ResultCode.id, articleCategory.getArticleCategoryId());
                articleCategoryInfo.put(ResultCode.CategoryName, articleCategory.getArticleCategoryName());
                articleCategoryInfo.put(ResultCode.update_at, articleCategory.getArticleCategoryUpdate_at());

                tableData.add(articleCategoryInfo);
            }
            resultMap.put(ResultCode.code, ResultCode.SUCCESS_CODE);
            resultMap.put(ResultCode.tableData, tableData);
            resultMap.put(ResultCode.message, ResultCode.SUCCESS_MESSAGE);
            return ResponseEntity.ok(resultMap);
        }else{
            // 文章分类为空
            resultMap.put(ResultCode.code, ResultCode.WRONG_CODE_HAVE_NO_CATEGORIES);
            resultMap.put(ResultCode.message, ResultCode.WRONG_MESSAGE_HAVE_NO_CATEGORIES);
        }
        return ResponseEntity.ok(resultMap);
    }
}
